package com.molichuxing.services.infrastructure.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.MapObjectConvertUtil;
import com.molichuxing.services.infrastructure.bean.CarsMileageWarnEnterpriseBean;
import com.molichuxing.services.infrastructure.dao.CarsMileageWarnEnterpriseMapper;
import com.molichuxing.services.infrastructure.dto.request.create.CarsMileageWarnEnterpriseDto;
import com.molichuxing.services.infrastructure.dto.response.CarsMileageWarnEnterpriseResDto;
import com.molichuxing.services.infrastructure.service.ICarsMileageWarnEnterpriseService;
import com.molichuxing.services.property.alarmTabEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业订单车辆超程报警表 服务实现类
 * </p>
 *
 * @author wanghualin
 * @since 2020-06-03
 */
@Service("carsMileageWarnEnterpriseService")
@Slf4j
public class CarsMileageWarnEnterpriseServiceImpl  implements ICarsMileageWarnEnterpriseService {

    @Autowired
    private CarsMileageWarnEnterpriseMapper carsMileageWarnEnterpriseMapper;

    @Override
    public Paged<CarsMileageWarnEnterpriseResDto> listCarsMileageWarnEnterprise(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        long count = carsMileageWarnEnterpriseMapper.countCarsMileageWarnEnterprise(map);
        List<CarsMileageWarnEnterpriseResDto> List = carsMileageWarnEnterpriseMapper.listCarsMileageWarnEnterprise(map, pageNum, pageSize);
        Paged<CarsMileageWarnEnterpriseResDto> dto=new Paged<>();
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);
        dto.setTotal(count);
        dto.setList(List);
        dto.setTab(getWarn());
        return dto;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveCarsMileageWarnEnterprise(CarsMileageWarnEnterpriseDto dto) {
        CarsMileageWarnEnterpriseBean bean = new CarsMileageWarnEnterpriseBean();
        BeanUtils.copyProperties(dto,bean);
        return carsMileageWarnEnterpriseMapper.insertSelective(bean);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateCarsMileageWarnEnterprise(CarsMileageWarnEnterpriseDto dto) {
        CarsMileageWarnEnterpriseBean bean = new CarsMileageWarnEnterpriseBean();
        BeanUtils.copyProperties(dto,bean);
        return carsMileageWarnEnterpriseMapper.updateByPrimaryKeySelective(bean);
    }

    @Override
    public CarsMileageWarnEnterpriseResDto getCarsMileageWarnEnterprise(Integer id) {

        CarsMileageWarnEnterpriseBean bean = carsMileageWarnEnterpriseMapper.selectByPrimaryKey(id);
       if (bean!=null){
           CarsMileageWarnEnterpriseResDto dto=new CarsMileageWarnEnterpriseResDto();
           BeanUtils.copyProperties(bean,dto);
           return dto;
       }

        return null;
    }

    private List<TabDto> getWarn() {
        List<TabDto> tabDtos = new ArrayList<>();
        Map<String,Long> map1 = carsMileageWarnEnterpriseMapper.countCarsMileageWarnEnterpriseTab();
        for (alarmTabEnum value : alarmTabEnum.values()) {
            if (!CollectionUtils.isEmpty(map1)&&map1.get(String.valueOf(value.getValue()))!=null){
                TabDto tabDto = new TabDto(value.getName(), value.getValue(), map1.get(String.valueOf(value.getValue())).intValue());
                tabDtos.add(tabDto);
            }else{
                TabDto tabDto = new TabDto(value.getName(), value.getValue(), 0);
                tabDtos.add(tabDto);
            }

        }
        return tabDtos;
    }
}
